package com.besttone.search;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

public class Client {
	// -------------------------------------------------------------------------
	private static Client instance;
	private static String screenSize = "";
	private static String deviceId = "";
	private static float  dencyRate = 1.0f;
	private static DisplayMetrics display;

	private Handler mHd;
	private ThreadPoolExecutor mThreadPoorForLocal;
	private ThreadPoolExecutor mThreadPoorForDownload;
	private ThreadPoolExecutor mThreadPoorForRequest;
	private Context mContext;
	
	private final static String Tag="Client";
	
//	public static UserInfo userInfo;
//	public static WordInfo wordInfo;
//	public static int 	dataType;
//	public static boolean turnType;
//	public static DrawCommon drawComm;
//	public static GuessCommon guessComm;
//	public static boolean rightWrong;

	// -------------------------------------------------------------------------
	private Client() {
		instance = this;
		mHd = new Handler();
		mThreadPoorForLocal = new ThreadPoolExecutor(3, 6, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));
		mThreadPoorForRequest = new ThreadPoolExecutor(4, 8, 15, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));
		mThreadPoorForDownload = new ThreadPoolExecutor(2, 5, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));
	}
	// -------------------------------------------------------------------------
	public static void release() {
		try {
			instance.mThreadPoorForLocal.shutdownNow();
			instance.mThreadPoorForRequest.shutdownNow();
			instance.mThreadPoorForDownload.shutdownNow();
		} catch (Exception e) {
			e.printStackTrace();
		}
		instance = null;
	}
	// -------------------------------------------------------------------------
	public static Client getInstance() {
		if (instance == null) {
			instance = new Client();
		}
		return instance;
	}

	// -------------------------------------------------------------------------
	public static Handler getHandler() {
		return getInstance().mHd;
	}
	// -------------------------------------------------------------------------
	public static void postRunnable(Runnable r) {
		getInstance().mHd.post(r);
	}
	// -------------------------------------------------------------------------
	public static ThreadPoolExecutor getThreadPoolForLocal() {
		return getInstance().mThreadPoorForLocal;
	}
	// -------------------------------------------------------------------------
	public static ThreadPoolExecutor getThreadPoolForRequest() {
		Log.d(Tag,"request");
		return getInstance().mThreadPoorForRequest;
	}
	// -------------------------------------------------------------------------
	public static ThreadPoolExecutor getThreadPoolForDownload() {
		return getInstance().mThreadPoorForDownload;
	}
	// -------------------------------------------------------------------------
	public static void initWithContext(Context context) {
		getInstance().mContext = context;
		display = new DisplayMetrics();
		WindowManager wm = (WindowManager) context.getSystemService( Context.WINDOW_SERVICE );
		if (null != wm) {
			wm.getDefaultDisplay().getMetrics( display );
			screenSize = display.widthPixels < display.heightPixels ? display.widthPixels + "x" + display.heightPixels : display.heightPixels
					+ "x" + display.widthPixels;
			dencyRate = display.density / 1.5f;
		}
		TelephonyManager tm = (TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE );
		if (null != tm) {
			deviceId = tm.getDeviceId();
			if (null == deviceId)
				deviceId = "";
		}
	}
	// -------------------------------------------------------------------------
	public static Context getContext() {
		return getInstance().mContext;
	}
	// -------------------------------------------------------------------------
	public static String getDeviceId() {
		return deviceId;
	}
	// -------------------------------------------------------------------------
	public static String getScreenSize() {
		return screenSize;
	}
	// -------------------------------------------------------------------------
	public static float getDencyParam() {
		return dencyRate;
	}
	// -------------------------------------------------------------------------
	public static DisplayMetrics getDisplayMetrics() {
		return display;
	}
	// -------------------------------------------------------------------------
	public static boolean decetNetworkOn() {
		try {
			ConnectivityManager cm = (ConnectivityManager) getInstance().mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (null != cm ) {
				NetworkInfo wi = cm.getActiveNetworkInfo();
				if (null != wi)
					return wi.isConnected();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean decetNetworkOn(Context c) {
		ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (null != cm ) {
			NetworkInfo info = cm.getActiveNetworkInfo();
			if (null != info)
				return info.isConnected();
		}
		return false;
	}

	public static Bundle getAppInfoBundle(Context context) {
		Bundle bundle = null;
		try {
			ApplicationInfo appInfo = ((Activity)context).getPackageManager().getApplicationInfo(((Activity)context).getPackageName(), PackageManager.GET_META_DATA);
			if (appInfo != null) {
				bundle = appInfo.metaData;
			
			}
			PackageInfo pinfo = ((Activity)context).getPackageManager().getPackageInfo(((Activity)context).getPackageName(), PackageManager.GET_CONFIGURATIONS);
			if(pinfo!=null)
			{
				bundle.putString("versionName", pinfo.versionName);
				bundle.putInt("versionCode", pinfo.versionCode);
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		
		return bundle;
	}

	/**
	 * 应用程序版本
	 * @param context
	 * @return
	 */
	public static int getVersion(Context context) {
		int uVersion = 0;
		Bundle bundle = getAppInfoBundle(context);
		if (bundle != null) {
			uVersion = bundle.getInt("versionCode");
		}
		return uVersion;
	}
}
